#pragma once

#include <assert.h>
#include "db/sqlite3/Sqlite3DB.h"
#include "common/DateTime.h"


inline void sqlite3_test()
{
    DBConfig config;
    config.SQLiteFile = "./sqlite.db3.db";

    Sqlite3DB db;

    assert(db.open(config));

    std::string create_sql = "CREATE TABLE mixtest ("
        "IntField  INTEGER NOT NULL DEFAULT 0,"
        "FloatField  REAL NOT NULL DEFAULT 0,"
        "StringField  TEXT NOT NULL DEFAULT '',"
        "DateField TEXT NOT NULL DEFAULT '0000-00-00 00:00:00'"
        ")";

    {
        db.exec("drop table mixtest");
        assert(db.exec(create_sql) >= 0);
    }

    {
        assert(1 == db.exec("insert into mixtest(IntField,FloatField,StringField,DateField) "
            "values(?,?,?,?)", 1, 3.14, "or 1=1", DateTime::currentDateTime().toString()));
        char str[] = "string";
        auto r = db.exec("insert into mixtest(IntField,FloatField,StringField,DateField) "
            "values(?,?,?,?)", 2, 2.22, str, DateTime::currentDateTime().toString());
        assert(r == 1);

    }
   
    {
        DBRows data;
        assert(db.query("select IntField,FloatField,StringField,DateField from mixtest", data));
        assert(data.size() == 2);
        for (auto it = data.begin(); it != data.end(); ++it) {
            int IntField;
            float FloatField;
            std::string StringField;
            char DateField[32] = {};
            assert(it->scan(IntField, FloatField, StringField, DateField));
            printf("%d %f %s %s\n", IntField, FloatField, StringField.c_str(), DateField);
        }
    }

    {
        assert(1 == db.exec("update mixtest set FloatField = ? where IntField = ?", 9.99, 1));
        DBRows data;
        assert(db.query("select FloatField from mixtest where IntField = 1", data));
        assert(data.size() == 1);
        assert(data[0]["FloatField"].to_double() == 9.99);
    }

    {
        assert(1 == db.exec("delete from mixtest where IntField = 1"));
    }




    std::cout << "sqlite3 test end " << 1 << std::endl;
}